/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.JavaXML.DOMValidator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.Nodes;
import nu.xom.Serializer;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author thanhtq
 */
public class CreateQuestionAndAnswer {
    public static final String QUESTION_XML = "D:\\Project\\thanhtv00011\\Survey\\question.xml";

    private Document doc = null;

    public Document createQuestion(Question question) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            Node nodes = doc.query("//document/questions/question").get(0);
            Element content = new Element("Content");
            content.appendChild(question.getContent());
            Element questionElement = new Element("question");
            Attribute id = new Attribute("id", question.getId());
            questionElement.addAttribute(id);
            questionElement.appendChild(content);
            nodes.getParent().fastInsertChild(questionElement, nodes.getParent().getChildCount());
            save();
            return doc;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public Document createAnswer(Answer answer) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            Node nodes = doc.query("//document/answers/answer").get(0);
            Element option = new Element("option");
            option.appendChild(answer.getOption());
            Element QuesID = new Element("QuesID");
            QuesID.appendChild(answer.getQuesId());
            Element rate = new Element("rate");
            rate.appendChild("0");
            Element answerElement = new Element("answer");
            Attribute id = new Attribute("id", answer.getId());
            answerElement.addAttribute(id);
            answerElement.appendChild(option);
            answerElement.appendChild(QuesID);
            answerElement.appendChild(rate);
            nodes.getParent().fastInsertChild(answerElement, nodes.getParent().getChildCount());
            save();
            return doc;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public static void main(String argm[]){
        
        Question q = new Question();
        q.setContent("question 5");
        q.setId("5");
        new CreateQuestionAndAnswer().createQuestion(q);

        Answer a = new Answer();
        a.setId("9");
        a.setOption("option 1");
        a.setQuesId("1");
        a.setRate("0");
        new CreateQuestionAndAnswer().createAnswer(a);
    }

    public void update(int rate,String id){

        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            System.out.println(rate+"//document/answers/answer[@id='"+id+"']/rate");
            XQueryUtil.update(doc, "//document/answers/answer[@id='"+id+"']/rate",String.valueOf(rate));
            save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void removeAnser(String id) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            Node nodes = doc.query("//document/answers/answer").get(0);
            Node node = doc.query("//document/answers/answer[@id='" + id + "']").get(0);
            remove(nodes, node);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void remove(Node nodeRoot, Node nodeRemove) {
        nodeRoot.getParent().removeChild(nodeRemove);
    }

    public void removeQuestion(String id) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            Node node = doc.query("//document/questions/question").get(0);

            Nodes nodes = doc.query("//document/answers/answer[QuesID='" + id + "']");
            for (int i = 0; i < nodes.size(); i++) {
                remove(doc.query("//document/answers/answer").get(0), nodes.get(i));
            }
            Node nodeChild = doc.query("//document/questions/question[@id='" + id + "']").get(0);
            node.getParent().removeChild(nodeChild);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void removeQuestionByCaid(String caId) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(QUESTION_XML));
            Nodes nodes = doc.query("//document/questions/question[CaID='" + caId + "']");
            for (int i = 0; i < nodes.size(); i++) {
                System.out.println(""+nodes.get(i).getParent().query("/question").get(0).getValue());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public boolean save() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(QUESTION_XML));
            Serializer ser = new Serializer(fos);
            ser.write(doc);
            ser.flush();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(CreateSurvey.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
